#coding=utf-8
import requests,json,urllib
import os
from bs4 import BeautifulSoup
import json
import codecs

#爬取爱彼迎房子图片
def get_photo():
    s = requests.session()
    index = s.get('https://www.airbnb.cn/api/v2/pdp_listing_details/32760124?_format=for_rooms_show&adults=1&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&')
    photo = index.json()["pdp_listing_detail"]["photos"]
    n = len(photo)
    for n in range(0,n):
        print photo[n]

#爬取爱彼迎日历
def get_calendar_months():
    s = requests.session()
    for month in range(3,12):
        index = s.get('https://www.airbnb.cn/api/v2/calendar_months?_format=with_conditions&count=4&currency=CNY&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&listing_id=32760124&locale=zh&month='+ str(month) +'&year=2019')
        calendar = index.json()["calendar_months"][0]["days"]
        n =len(calendar)
        for n in range(0,n):
            print calendar[n]

#爬取成都博物馆
def museum():
    s = requests.session()
    index = s.get('https://www.gochengdu.cn/zh2016/travel/attractions/museums-c257p1.html')
    index_soup = BeautifulSoup(index.text, 'html.parser')
    museum = index_soup.find_all('h4')
    image = index_soup.find_all('img',border='0')
    detail = index_soup.find_all('div',class_='text')
    href = index_soup.find_all('a', href=True,text='详情')
    n = len(image)
    for n in range(0,n):
        newmuseum = museum[n].string
        newimage = image[n]['src']
        newdetail = detail[n].string
        newhref = href[n]['href']
        detail_index = s.get(str(newhref))
        detail_index.encoding = 'utf-8'
        detail_soup = BeautifulSoup(detail_index.content, 'html.parser')
        # print newhref,newmuseum,newimage,newdetail
        detail01 = detail_soup.find('div',class_='cont_t')
        detail02 = detail_soup.find_all('div', class_='content_text')
        # print detail01.get_text()+detail02[0].text
        write_json(newmuseum,'https://www.gochengdu.cn/'+newimage,newdetail,detail01.get_text()+detail02[0].text)


#将结果写入到json文件
def write_json(*args):
    os.chdir('D:\\python-photo\\log')  # 切换到当前目录
    report = open("museum.json", "a")  # 打开报告
    file_path = 'D:\\python-photo\\log\\museum.json'
    data = {}
    data['name'] = args[0]
    data['image_href'] = args[1]
    data['describe'] = args[2]
    data['detail'] = args[3]
    try:
        with codecs.open(file_path,'a',encoding='utf-8') as fp:
            fp.write(json.dumps(data, ensure_ascii=False) + ",\n")
    except IOError as err:
        print('error' + str(err))
    finally:
        fp.close()
if __name__ == '__main__':
    # get_calendar_months()
    museum()